#pragma once
#include "transferbox.h"
#include "chatwidget.h"
#include <QByteArray>
#include <QDataStream>
#include <QLineEdit>
#include <QPlainTextEdit>
#include <QTcpSocket>
#include <QSplitter>

class Connection : public QSplitter
{
    Q_OBJECT
public:
    //this will be casted to 8 bits. Must be <256 values
    enum BlockFlag { INVALID = 0,
                     ChatStringFlag, ChatCharFlag, ChatBackspaceFlag, ChatEnterFlag, ChatMoveFlag,
                     FileInitiateFlag, FileDataFlag,  FileFinishFlag, FileCancelFlag };
    Connection(QTcpSocket *tcpSocket, QWidget *parent = 0);
    ~Connection();
private slots:
    void stateChange(QAbstractSocket::SocketState socketState);
    void afterSent(qint64 bytes);
    void receiveData();
    void endConnection();
    void locateFile();
    void transferFinished(int id);
    void removeReceivingTransferBox(int id);
    void removeSendingTransferBox(int id);
    void transferCanceled(int id);

    void sendBackspace();
    void sendChar(QChar msg);
    void sendDelete();
    void sendString(QString msg);
    void sendMsg();
    void sendMove(qint16 dir);

private:
    void sendData(QByteArray data, BlockFlag flag);
private:
    QTcpSocket *socket;
    QDataStream streamer;
    ChatWidget *chatBox;
    QMap<int, TransferBox*> receivingTransfers;
    int sendingIdUpto;
    QMap<int, TransferBox*> sendingTransfers;
    QPushButton *sendFileButton;
    QVBoxLayout *transferLay;


    //for reading persistent variables
    quint8 blockType;
};
